package com.iLoo.controller;

import com.iLoo.entity.User;
import com.iLoo.service.UserService;
import com.iLoo.utils.VerifyCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@CrossOrigin //允许跨域
//@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    VerifyCodeUtils verifyCode;

    @Autowired
    private UserService userService;

    //@RequestMapping("quit")
    //public String quit(HttpServletRequest request){
    //
    //}

    /**
     *用来处理用户登录
     * @param user
     * @return
     */
    @RequestMapping("loginForm")
    public String login(User user,String code,HttpServletRequest request,Model model){
        String key = (String)request.getServletContext().getAttribute("code");
        log.info("真实验证码为：{}",key);
        if(key==null){
            model.addAttribute("state", false);
            model.addAttribute("msg", "请先输入验证码");
            return "login";
        }
        if(key.equalsIgnoreCase(code)) {
            try {
                User userDB = userService.login(user);
                model.addAttribute("state", true);
                model.addAttribute("msg", "登陆成功！");
                model.addAttribute("user", userDB);
                request.getServletContext().setAttribute("user",userDB);
                request.getServletContext().setAttribute("username",userDB.getUsername());
                //登录成功重定向到main.html;  重定向防止表单重复提交
                //这里因为用了重定向，所以要写完整页面路径了
                return "redirect:/findAll";
            } catch (Exception e) {
                e.printStackTrace();
                model.addAttribute("state", false);
                model.addAttribute("msg", e.getMessage());
                return "login";
            }
        }else {
            model.addAttribute("msg","提示：验证码出现错误！");
            return "login";
        }

    }



    /**
     * 用来处理用户注册方法
     */
    @PostMapping("registerForm")
    //参数中的注释先暂存，我记得是可以直接赋值的呀，up主说了什么名词后说是json传输才不可以的，不知道是不是vue的问题
    public String register(/*@RequestBody*/ User user, String code, HttpServletRequest request, Model model){
        log.info("用户信息：[{}]",user.toString());
        log.info("用户输入的验证码信息：[{}]",code);
        //1.调用业务方法

        String key = (String)request.getServletContext().getAttribute("code");
        log.info("真实验证码为：{}",key);
        if(key.equalsIgnoreCase(code)){
            boolean result=userService.register(user);
            if(result){
                model.addAttribute("state",true);
                request.getServletContext().setAttribute("msg","提示：注册成功！");
                request.getServletContext().setAttribute("user",user);
                //model.addAttribute("msg","提示：注册成功！");
                //model.addAttribute("user",user);

                return "redirect:/login";
            }else {
                model.addAttribute("msg","提示：用户名已存在！");
                return "register";
            }


        }else {
            model.addAttribute("msg","提示：验证码出现错误！");
            return "register";
        }

    }

    /**
     * 跳转界面
     * @return
     */
    @GetMapping("quit")
    public String quit(HttpServletRequest request) {
        request.getServletContext().setAttribute("user",null);
        return "redirect:/login";
    }

    /**
     * 跳转界面
     * @return
     */
    @GetMapping("login")
    public String loginHtml() {
        return "login";
    }

    /**
     * 跳转界面
     * @return
     */
    @RequestMapping("register")
    public String registerHtml(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //verifyCode.setVerifyCode(request,response);
        return "register";
    }

    /**
     * 生成验证码
     * @return
     */
    @GetMapping("getImage")
    public void getImageCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.使用工具类生成验证码

        //2.将验证码放入servletContext作用域
        verifyCode.setVerifyCode(request,response);
        //request.getServletContext().setAttribute("code",code);
        System.out.println(request.getServletContext().getAttribute("code"));

        //return "login";
    }

    @RequestMapping("/")
    public String indexlogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //verifyCode.setVerifyCode(request,response);
        return "login";
    }

    //@RequestMapping("/emplist")
    //public String indexlogin()  {
    //    //verifyCode.setVerifyCode(request,response);
    //    return "emplist";
    //}

    @RequestMapping("success")
    public String success()  {

        return "success";
    }


    /**
     * 我测试一下redis
     * @param user
     * @param code
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("testRedis")
    //参数中的注释先暂存，我记得是可以直接赋值的呀，up主说了什么名词后说是json传输才不可以的，不知道是不是vue的问题
    public String testRedis(/*@RequestBody*/ User user, String code, HttpServletRequest request, Model model){

        User userResult = userService.getById(2);

        model.addAttribute("state", true);

        model.addAttribute("msg", "提示：注册成功！");
        model.addAttribute("user", userResult);

        return "success";

    }

}
